//
//  BindTelViewModel.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/6/5.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class BindTelViewModel: BaseViewModel {
     open var model:SecurityModel? //传递的model
     open var bindGoogleBool = false //是否绑定谷歌验证
     var telText = "" //手机号
     var codeText = "" //验证码
     var googleCode = "" //谷歌验证码
     var oldCodeText = "" //原手机号验证码 或 邮箱验证码
     var oldTelBool = false //原手机号短信
    var countryCode = "" //国家标记
    
    lazy var codeRequest:MobileCodeRequest = {
        let api = MobileCodeRequest()
        api.dataReformer = self
        api.paramSource = self
        api.delegate = self
        return api
    }()
    
    lazy var bindTelRequest:BindTelRequest = {
        let api = BindTelRequest()
        api.dataReformer = self
        api.paramSource = self
        api.delegate = self
        return api
    }()
    
    lazy var emailRequest:EmailCodeRequest = {
        let api = EmailCodeRequest()
        api.dataReformer = self
        api.paramSource = self
        api.delegate = self
        return api
    }()
    
    override init(controller: BaseViewController) {
        super.init(controller: controller)
        countryCode = "86"
    }
    
    func putInformation() {
        self.request(request: bindTelRequest, success: {[weak self] (result) in
            if AppSecurityInfo.default.model.isMobileBind {
                CYMBShow.showMessage(kText("手机号换绑成功,请重新登录"))
                AppInfo.default.isLogin = false
                self?.controller?.navigationController?.popToRootViewController(animated: true)
            }else {
                self?.controller?.navigationController?.popViewController(animated: true)
                NotificationCenter.default.post(name: NSNotification.Name.Exchange.securityDidChange, object: nil)
                CYMBShow.showMessage(kText("手机号绑定成功"))
            }
        }) { (request) in
            CYMBShow.showMessage(request.errorMsg)
        }
    }
    
    /// 发送验证码
    ///
    ///
    /// - Parameter stateBool: 是否是原先手机号
    func sendCode(stateBool:Bool,btn: TimerBtn)  {
        oldTelBool = stateBool
        if !oldTelBool {
            if telText.count == 0 {
                CYMBShow.showMessage(kText("请填写手机号码"))
                return
            }
        }
        btn.isEnabled = false
        self.request(request: codeRequest, success: { (result) in
            btn.timeStart()
        }) { (request) in
            if Helper.checkTimerCode(response:request.response) {
                btn.timeStart()
            }else {
                btn.isEnabled = true
            }
            CYMBShow.showMessage(request.errorMsg)
            
        }
    }
    func sendEmailCode(btn:TimerBtn) {
        if telText.count == 0 {
            CYMBShow.showMessage(kText("请填写邮箱"))
            return
        }
        btn.isEnabled = false
        self.request(request: emailRequest, success: { (result) in
            btn.timeStart()
        }) { (request) in
            if Helper.checkTimerCode(response:request.response) {
                btn.timeStart()
            }else {
                btn.isEnabled = true
            }
            CYMBShow.showMessage(request.errorMsg)
        }
    }
    
    func archiveCellKind() {
        var array:[CYTableviewItem] = []
        let itemFir = CYTableviewItem(type: .cyItemCountry, name: "", placeHolder: kText("请输入手机号"))
        array.append(itemFir)
        let itemSec = CYTableviewItem(type: .cyItemTimer, name: kText("短信验证码"), placeHolder: kText("请输入短信验证码"))
        array.append(itemSec)
        var itemThird:CYTableviewItem
        if AppSecurityInfo.default.model.isMobileBind {
           itemThird = CYTableviewItem(type: .cyItemTimer, name: kText("原验证码"), placeHolder: kText("请输入原手机短信验证码"))
        }else {
           itemThird = CYTableviewItem(type: .cyItemTimer, name: kText("邮箱验证码"), placeHolder: kText("请输入邮箱短信验证码"))
        }
        array.append(itemThird)
        if AppSecurityInfo.default.model.isOpenGoogle {
            let itemFource = CYTableviewItem(type: .cyItemNumber, name: kText("谷歌验证码"), placeHolder: kText("请输入谷歌验证码"))
           array.append(itemFource)
        }
        cellfactory.iteamArray.append(array)
        
    }
}

extension BindTelViewModel:RequestCallBack,RequestDataReformer,RequestParamSource {
    func reform(_ request: BaseRequest, _ data: [String : Any]?) {
        if successBlock != nil {
            successBlock!(data)
        }
    }
    
    func requestDidSucess(_ request: BaseRequest) {
        
    }
    
    func requestDidFail(_ request: BaseRequest) {
        if failBlock != nil {
            failBlock!(request)
        }
    }
    
    func requestParams(_ request: BaseRequest) -> [String : Any]? {
        var pramas: [String:Any] = [:]
        if let _ = request as? MobileCodeRequest {
            pramas.updateValue(AppSecurityInfo.default.model.areaCode.ch_length != 0 ? AppSecurityInfo.default.model.areaCode as Any : "" , forKey: "areaCode")
            if oldTelBool {
                pramas.updateValue(AppSecurityInfo.default.model.mobile.ch_length != 0 ? AppSecurityInfo.default.model.mobile as Any : "", forKey: "mobile")
                pramas.updateValue(9, forKey: "codeType")
            }else {
                pramas.updateValue(telText, forKey: "mobile")
                pramas.updateValue(5, forKey: "codeType")
            }
        }else if let _ = request as? BindTelRequest {
            pramas.updateValue(5, forKey: "codeType")
            pramas.updateValue(countryCode, forKey: "areaCode")
            pramas.updateValue(telText, forKey: "mobile")
            pramas.updateValue(codeText, forKey: "mobileCode")
            if AppSecurityInfo.default.model.isMobileBind {
                pramas.updateValue(AppSecurityInfo.default.model.mobile.ch_length != 0 ? AppSecurityInfo.default.model.mobile as Any : "", forKey: "oldMobile")
                pramas.updateValue(oldCodeText, forKey: "oldMobileCode")
            }else {
                pramas.updateValue(oldCodeText, forKey: "emailCode")
                pramas.updateValue(AppSecurityInfo.default.model.email.ch_length != 0 ? AppSecurityInfo.default.model.email as Any : "", forKey: "email")
                
            }
            if (AppSecurityInfo.default.model.isEmailBind) {
                pramas.updateValue(1, forKey: "type")
            }
            if AppSecurityInfo.default.model.isMobileBind {
                pramas.updateValue(0, forKey: "type")
            }
            if bindGoogleBool {
                pramas.updateValue(2, forKey: "type")
                pramas.updateValue(googleCode, forKey: "googleCode")
            }
        }else if let _ = request as? EmailCodeRequest {
            pramas.updateValue(5, forKey: "codeType")
            pramas.updateValue(AppSecurityInfo.default.model.areaCode.ch_length != 0 ? AppSecurityInfo.default.model.areaCode as Any : "" , forKey: "areaCode")
            pramas.updateValue(AppSecurityInfo.default.model.email.ch_length != 0 ? AppSecurityInfo.default.model.email as Any : "", forKey: "email")
            pramas.updateValue(1, forKey: "type")
        }
        
        return pramas
    }
}
